BUILD_OS=`uname -s`
BUILD_ARCH=`uname -m`

if [ "$BUILD_OS" = "Linux" ]; then
    LINUXOS=`if [ -d /usr/src/redhat ]; then echo "redhat";else echo "suse";fi`
fi

# If HBAnyware running, then tell user to exit it before continuing
pid=`ps -eaf | grep HBAnyware.jar | grep -v grep | grep -v start | awk '{ print $2 }' | sort -n | head -1`
if [ ! -z "$pid" ];then
    echo " "
    echo "The HBAnyware utility is currently running.  Before installing"
    echo "Web Launch Services, you must first stop the HBAnyware utility"
    echo "by performing the following steps:"
    echo " "
    echo "  - Exit the HBAnyware utility"
    echo "  - Stop all HBAnyware services by executing the shell"
    echo "    script '/usr/sbin/hbanyware/stop_hbanyware -n'"
    echo "  - Re-execute this script (wsinstall)"
    echo " "
    exit 0 
fi

# Get the web server document root directory
# DOCUMENT_ROOT=/usr/local/apache2/htdocs
echo ""
echo -n "Enter your web server's document root directory: "
read DOCUMENT_ROOT
if [ ! -d ${DOCUMENT_ROOT} ]; then
    echo ""
    echo "The specified document root directory '"${DOCUMENT_ROOT}" 'does not exist!";
    echo ""
    echo -n "Please re-enter your web server's document root directory: "
    read DOCUMENT_ROOT
    if [ ! -d ${DOCUMENT_ROOT} ]; then
    echo ""
       echo "The specified document root directory '"${DOCUMENT_ROOT}"' does not exist!";
       echo "Please determine you web server's document root directory, then retry the installation."
       echo ""
       exit 0 
    fi
fi

echo ""
echo ${DOCUMENT_ROOT} > /usr/sbin/hbanyware/document_root

if [ ! -d ${DOCUMENT_ROOT}/HBAnyware ]; then
    /bin/mkdir ${DOCUMENT_ROOT}/HBAnyware
fi 

# Embed the approriate URL in the various dynamic files
/usr/sbin/hbanyware/updateWsUrls

# Copy the HBAnyware JNLP file to the document root directory
cp -f /usr/sbin/hbanyware/hbanyware.jnlp ${DOCUMENT_ROOT}/hbanyware.jnlp
chmod 644 ${DOCUMENT_ROOT}/hbanyware.jnlp

# Change the permissions on the rmiserver script
chmod 744 /usr/sbin/hbanyware/rmiserver

# Create a symbolic link in the web server document root directory which
# points to the release version of HBAnyware
#ln -sf /usr/sbin/hbanyware/HBAnyware.jar ${DOCUMENT_ROOT}/HBAnyware/HBAnyware.jar

# Just copy HBAnyware.jar for now
cp -f /usr/sbin/hbanyware/HBAnyware.jar ${DOCUMENT_ROOT}/HBAnyware

# Create a symbolic link in the web server document root directory which
# points to the release version of HBAnyware help
# ln -sf /usr/sbin/hbanyware/hbanyware_help ${DOCUMENT_ROOT}/HBAnyware/hbanyware_help

# Just copy Help directory for now
cp -rf /usr/sbin/hbanyware/hbanyware_help ${DOCUMENT_ROOT}/HBAnyware

# Copy the web launch server startup file to the startup directory
cp -f /usr/sbin/hbanyware/ElxWSSrv /etc/init.d/ElxWSSrv

# Set up start up link
if [ "$BUILD_OS" = "Linux" ]; then

   # Config library
   ldconfig

   if [ "$LINUXOS" = "redhat" ];then
      /sbin/chkconfig --add ElxWSSrv
   else
      /sbin/chkconfig --add ElxWSSrv
      /sbin/chkconfig ElxWSSrv 235
   fi

   # Start up the web launch daemons (rmiserver and, if necessary, rmiregistry)
   /usr/sbin/hbanyware/start_weblaunch

   echo "Installation of the HBAnyware Web Launch package has completed!"

else
   # Solaris
   ln -s /etc/init.d/ElxWSSrv /etc/rc0.d/K10ElxWSSrv
   ln -s /etc/init.d/ElxWSSrv /etc/rc2.d/S99ElxWSSrv

   echo " "
   echo "Installation of the HBAnyware Web Launch package has completed!"
   echo " "
   echo "Please be sure to reboot this computer before attempting to use HBAnyware Web Launch services!"
   echo " "
fi


